package net.sea.setup.ui.wizard.impl;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import java.util.Map;

import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import net.miginfocom.swing.MigLayout;

import net.sea.commons.constants.AppConstants;
import net.sea.commons.util.StringUtils;
import net.sea.setup.config.custom.CustomConfig;
import net.sea.setup.ui.wizard.AbstractWizardPage;

/**
 * 许可证页面
 * 
 * @author 华成伍
 *
 */
@SuppressWarnings("serial")
public class LicensePage extends AbstractWizardPage {
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private static boolean acceptStatus = true;
	// 同意单选按钮
	private JRadioButton agreeRadioBtn = new JRadioButton("\u6211\u63A5\u53D7");
	// 不同意单选按钮
	private JRadioButton disagreeRadioBtn = new JRadioButton(
			"\u6211\u4E0D\u63A5\u53D7");

	/**
	 * 创建许可证页面容器
	 */
	@SuppressWarnings({ "unchecked", "deprecation" })
	public LicensePage() {
		setBorder("\u8BB8\u53EF\u8BF4\u660E");
		setLayout(new MigLayout("", "[][grow]", "[][grow][]"));

		JLabel tipLb = new JLabel(
				"\u7EE7\u7EED\u5B89\u88C5\u4F7F\u7528\u6613\u6D4B\u5E73\u53F0\u9700\u8981\u63A5\u53D7\u4EE5\u4E0B\u534F\u8BAE\uFF1A");
		add(tipLb, "cell 1 0");

		JScrollPane licenseScrollPane = new JScrollPane();
		add(licenseScrollPane, "cell 1 1,grow");
		// 获取文件数据
		Map<String, String> dataMap = (Map<String, String>) contextInst
				.getAttribute(AppConstants.DATA_FILE);
		String dataFile = "";
		if (dataMap != null) {
			dataFile = dataMap.get(this.getClass().getName());
		}
		JTextPane licenseTextPanel = new JTextPane();
		licenseTextPanel.setEditable(false);
		licenseTextPanel.setOpaque(false);
		// licenseTextPanel.setContentType("text/html");
		// licenseTextPanel.setText(dataFile == null ? "" : dataFile);
		try {
			if (StringUtils.isNotEmpty(dataFile)) {
				File licenseFile = new File(System.getProperty("current_dir"),
						dataFile);
				licenseTextPanel.setPage(licenseFile.toURL());
			}
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		licenseScrollPane.setViewportView(licenseTextPanel);

		agreeRadioBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				acceptStatus = true;
			}
		});
		agreeRadioBtn.setSelected(true);
		agreeRadioBtn.setOpaque(false);
		buttonGroup.add(agreeRadioBtn);
		add(agreeRadioBtn, "cell 1 2");

		disagreeRadioBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				acceptStatus = false;
			}
		});
		buttonGroup.add(disagreeRadioBtn);
		disagreeRadioBtn.setOpaque(false);
		add(disagreeRadioBtn, "cell 1 2");
	}

	@Override
	public PageResult validateInput() {
		return new PageResult(acceptStatus);
	}

	@Override
	protected List<CustomConfig> createCustomConfig() {
		return null;
	}

	@Override
	public void done() {
		super.done();
		if (disagreeRadioBtn.isSelected()) {
			Object[] options = { "确定", "取消" };
			JOptionPane opPanel = new JOptionPane("是否要退出安装程序?",
					JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION,
					null, options, options[1]);
			JDialog dialog = opPanel.createDialog(getParent(),
					AppConstants.MESSAGE_DIALOG_TITLE);
			dialog.setVisible(true);
			Object selectedValue = opPanel.getValue();
			if (selectedValue == options[0]) {
				System.exit(0);
			} else {
				dialog.dispose();
			}
		}
	}
}
